import { defineStore } from 'pinia'
import { useAccountLoginApi, useMobileLoginApi, useLogoutApi, useThirdLoginApi, useLoginApi } from '@/api/auth'
import { useUserInfoApi } from '@/api/sys/user'
import cache from '@/utils/cache'
import { useAuthorityListApi } from '@/api/sys/menu'

export const useUserStore = defineStore('userStore', {
	state: () => ({
		// 用户信息
		user: {
			id: '',
			superAdmin: 0,
			username: '',
			realName: '',
			gender: 0,
			mobile: '',
			email: '',
			avatar: '',
			orgName: '',
			postNameList: [],
			createTime: '',
			roleCode: ''
		},
		// 权限列表
		authorityList: [],
		// 访问token
		token: cache.getToken(),
		// 刷新token
		refreshToken: cache.getRefreshToken()
	}),
	actions: {
		setUser(val: any) {
			const data = {
				id: val.userId,
				superAdmin: 0,
				username: val.userName,
				realName: val.userName,
				gender: 0,
				mobile: '',
				email: '',
				avatar: val.avatar,
				orgName: val.dwmc,
				postNameList: [],
				createTime: '',
				roleCode: val.roleCode
			}

			this.user = data
		},
		setToken(val: any) {
			this.token = val
			cache.setToken(val)
		},
		setRefreshToken(val: any) {
			this.refreshToken = val
			cache.setRefreshToken(val)
		},
		// 账号登录
		// async accountLoginAction(loginForm: any) {
		// 	const { data } = await useAccountLoginApi(loginForm)
		// 	this.setToken(data.access_token)
		// 	this.setRefreshToken(data.refresh_token)
		// },

		async accountLoginAction(loginForm: any) {
			const { data } = await useLoginApi(loginForm)
			this.setToken(data.token)
			this.setRefreshToken(data.token)
		},

		// 手机号登录
		async mobileLoginAction(loginForm: any) {
			const { data } = await useMobileLoginApi(loginForm)
			this.setToken(data.access_token)
			this.setRefreshToken(data.refresh_token)
		},
		// 第三方登录
		async thirdLoginAction(loginForm: any) {
			const { data } = await useThirdLoginApi(loginForm)
			this.setToken(data.access_token)
			this.setRefreshToken(data.refresh_token)
		},
		// 获取用户信息
		async getUserInfoAction() {
			const { data } = await useUserInfoApi()
			this.setUser(data)

			const arry = extractMenuCodesFromButtons(data.menuList)

			this.authorityList = arry || []

			console.log(this.authorityList, 'sgsgsgsgsgw')
		},
		// 获取权限
		// async getAuthorityListAction() {
		// 	const { data } = await useAuthorityListApi()
		// 	//this.authorityList = data || []
		// },
		// 用户退出
		async logoutAction() {
			///	await useLogoutApi()

			// 移除 token
			this.setToken(null)
			this.setRefreshToken(null)
		}
	}
})

function extractMenuCodesFromButtons(data) {
	let result = []
	if (!Array.isArray(data)) return result

	data.forEach(item => {
		// 检查当前对象是否有 button 数组
		if (item && Array.isArray(item.button)) {
			item.button.forEach(button => {
				if (button && button.menuCode !== undefined) {
					result.push(button.menuCode)
				}
			})
		}
		// 递归处理子数组(children)
		if (item && Array.isArray(item.children)) {
			result = result.concat(extractMenuCodesFromButtons(item.children))
		}
	})

	return result
}
